<?php
header('Content-Type: text/html; charset=UTF-8');
require 'crawl.php';
require 'database.php';
$db = new DB();

// Select games ID
$Qry = 'SELECT g.id, g.fix_url
		FROM games g
		INNER JOIN games_description d ON (g.id = d.id)
		WHERE d.infomation IS NULL
		ORDER BY g.id DESC
		LIMIT 10';
$res = $db->fetchAll($Qry);

foreach ($res as $key=>$value) {
	crawl_2($value['id'], $value['fix_url']);
}

function crawl_2($id, $url) {
	global $db;

	$url = 'http://www.oyungemisi.com' . $url;

	if (file_get_html($url) == false) {
		echo 'Error';
		$del = 'DELETE FROM games WHERE id = ' . $id;
		$db->Query($del);

		$del2 = 'DELETE FROM games_description WHERE id = ' . $id;
		$db->Query($del2);
	} else {
		// play
		$_html = file_get_html($url);
		$_string = $_html->find('div#playnow', 0)->innertext;

		$_str = str_get_html($_string);
		foreach ($_str->find('a') as $element){
			$link = trim($element->href);
		}

		// Infomation
		$html = file_get_html($url);

		if ($html->find('div.game-info', 0))
			$string = $html->find('div.game-info', 0)->innertext;
		else
			$string = '';

		if (!$string) {
			return false;
			$html->clear();
			unset($html);
		}

		$str = str_get_html($string);

		foreach ($str->find('h2') as $e)
			$e->outertext = '';
		foreach ($str->find('a') as $e)
			$e->outertext = '';

		$description = $str->find('p itemprop="description"', 0)->innertext;
		$description = trim($description);
		$description = str_replace("'", '', $description);

		$str = str_replace('<p itemprop="description">', '<p id="description">', $str);
		$howto = str_get_html($str);

		foreach ($howto->find('p#description') as $e)
			$e->outertext = '';

		$howto = strip_tags($howto);
		$howto = trim($howto);
		$howto = str_replace("'", '', $howto);

		$_Qry = 'UPDATE games SET crawl_url = "' . $link . '" WHERE id = ' . $id;
		$db->Query($_Qry);
		echo '<h1>' . $_Qry . '</h1>';

		$query = "UPDATE games_description SET infomation = '" . $description . "', howtoplay = '" . $howto . "' WHERE id = " . $id;
		$db->Query($query);

		echo '<div>' . $query . '</div>';
	}
}

function crawl($id, $url) {

	$link = 'http://www.oyuncuyuz.biz.tr/oyna/' . $url . '.html';

	$html = file_get_html($link);

	foreach ($html->find('style') as $e)
		$e->outertext = '';

	foreach ($html->find('div.baslik') as $e)
		$e->outertext = '';

	if ($html->find('div.oyunaciklamasi', 0))
		$string = $html->find('div.oyunaciklamasi', 0)->innertext;
	else
		$string = '';

	if (!$string) {
		return false;
		$html->clear();
		unset($html);
	}

	$str = str_get_html($string);

	$howto = '';
	if ($str->find('fieldset', 0)) {
		$_howto = $str->find('fieldset', 0)->innertext;

		$_str = str_get_html($_howto);

		foreach ($_str->find('img') as $element) {
			$howto .= '<img src="' . $element->src . '" class="howto" />';
		}

		foreach ($str->find('fieldset') as $e)
			$e->outertext = '';
	}

	$str = strip_tags($str);
	$str = trim($str);
	$str = str_replace("'", '', $str);

	// echo $howto . $str . '<br /><br />';

	if ($str)
		$query = "UPDATE games_description SET infomation = '" . $str . "', howtoplay = '" . $howto . "' WHERE id = " . $id;

	return $query;
}